<template>
    <view class="container">
        <!-- 修改 loading 组件的 type -->
        <wd-loading v-if="loading" type="ring" color="#ff4444" />

        <template v-else>
            <!-- 商品轮播图区域 -->
            <view class="section">
                <view class="section-inner">
                    <swiper class="product-swiper" :circular="false" :indicator-dots="true" :autoplay="false"
                        @change="handleSwiperChange" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
                        <swiper-item>
                            <image :src="productDetail.mainImageUrl" mode="aspectFill" class="swiper-image" />
                        </swiper-item>
                        <swiper-item v-for="(image, index) in displayImages" :key="index">
                            <image :src="image" mode="aspectFill" class="swiper-image" />
                        </swiper-item>
                    </swiper>
                </view>
            </view>

            <!-- 商品信息区域 -->
            <view class="section">
                <view class="section-inner">
                    <view class="price-row">
                        <view class="price-section">
                            <text class="price-symbol">¥</text>
                            <text class="price-value">{{ productDetail.price }}</text>
                            <text class="original-price" v-if="productDetail.originalPrice">¥{{
                                productDetail.originalPrice }}</text>
                        </view>
                        <view class="sales-info">已售 {{ productDetail.salesCount || 0 }}</view>
                    </view>

                    <!-- 添加优惠券展示区域 -->
                    <view class="coupons-row" v-if="productDetail.coupons && productDetail.coupons.length"
                        @click="showCouponsPopup = true">
                        <view class="coupon-list">
                            <view class="coupon-tag" v-for="coupon in productDetail.coupons" :key="coupon.id" :class="{
                                'default-use': coupon.defaultUse,
                                'discount-type': coupon.discountType === 2
                            }">
                                <text class="coupon-name">{{ coupon.name }}</text>
                            </view>
                        </view>
                        <view class="coupon-arrow">
                            <wd-icon name="arrow-right" size="28rpx" color="#999" />
                        </view>
                    </view>

                    <view class="title-row">
                        <text class="product-name">{{ productDetail.name }}</text>
                    </view>

                    <view class="tags-row" v-if="productDetail.brandName || productDetail.categoryName">
                        <text class="tag brand-tag" v-if="productDetail.brandName">{{ productDetail.brandName }}</text>
                        <text class="tag category-tag" v-if="productDetail.categoryName">{{ productDetail.categoryName
                            }}</text>
                    </view>

                    <!-- 修改参数预览部分 -->
                    <view class="params-preview" v-if="hasParams" @click="showParams = true">
                        <view class="preview-params">
                            <view class="params-list">
                                <view class="param-item" v-if="productDetail.modelNumber">
                                    <text class="param-value">{{ productDetail.modelNumber }}</text>
                                </view>
                                <view class="param-item" v-for="(param, index) in previewParameters" :key="index">
                                    <text class="param-value">{{ param.split('：')[1] }}</text>
                                </view>
                            </view>
                            <view class="expand-icon">
                                <wd-icon name="arrow-down" size="24rpx" color="#999" />
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <!-- 评价预览区域 -->
            <view class="section">
                <view class="section-inner">
                    <view class="section-header">
                        <view class="left">
                            <text class="title">商品评价</text>
                            <text class="count" v-if="commentStats.total > 0">({{ commentStats.total }}条)</text>
                        </view>
                        <view class="right" v-if="commentStats.total > 0" @click="viewAllComments">
                            <text>查看全部</text>
                            <wd-icon name="arrow-right" />
                        </view>
                    </view>

                    <!-- 有评价时显示评分信息 -->
                    <template v-if="commentStats.total > 0">
                        <view class="rating-brief">
                            <view class="rating-left">
                                <text class="rating-score">{{ formatPrice(commentStats.averageRating) }}</text>
                                <view class="rating-stars">
                                    <wd-rate v-model="commentStats.averageRating" readonly size="14px" color="#eee"
                                        void-color="#ff4444" />
                                </view>
                            </view>
                            <view class="rating-right">
                                <text class="rating-desc">好评率 {{ commentStats.goodRatingPercentage }}%</text>
                            </view>
                        </view>

                        <!-- 最新两条评论 -->
                        <view class="comment-preview" v-if="comments && comments.length">
                            <view class="comment-item" v-for="comment in comments.slice(0, 2)" :key="comment.id">
                                <view class="user-info">
                                    <image :src="comment.user.avatar" class="avatar" />
                                    <text class="nickname">{{ comment.user.nickname }}</text>
                                    <view class="rating-stars">
                                        <wd-rate v-model="comment.rating" readonly size="12px" color="#eee"
                                            void-color="#ff4444" />
                                    </view>
                                </view>
                                <view class="comment-content">{{ comment.content }}</view>
                                <view class="comment-images" v-if="comment.images && comment.images.length">
                                    <image v-for="(image, index) in comment.images.slice(0, 3)" :key="index"
                                        :src="image" mode="aspectFill" class="comment-image" />
                                </view>
                            </view>
                        </view>
                    </template>

                    <!-- 无评价时显示 -->
                    <view v-else class="no-comment">
                        <text class="no-comment-text">暂无评价</text>
                    </view>
                </view>
            </view>

            <!-- 商品特点 -->
            <view class="section" v-if="productDetail.features">
                <view class="section-inner">
                    <view class="section-header">
                        <text class="title">商品特点</text>
                    </view>
                    <view class="features-list">
                        <view class="feature-item" v-for="(feature, index) in productDetail.features.split('，')"
                            :key="index">
                            <wd-icon name="check-round" color="#67c23a" size="32rpx" />
                            <text class="feature-text">{{ feature }}</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- 商品详情图片 -->
            <view class="section" v-if="productDetail.detailImagesUrl && productDetail.detailImagesUrl.length">
                <view class="section-inner">
                    <view class="section-header">
                        <text class="title">详情展示</text>
                    </view>
                    <view class="detail-images">
                        <image v-for="(image, index) in productDetail.detailImagesUrl" :key="index" :src="image"
                            mode="widthFix" class="detail-image" />
                    </view>
                </view>
            </view>

            <!-- 添加遮罩层 -->
            <wd-overlay v-model="loadingOverlay" :z-index="100">
                <view class="loading-content">
                    <wd-loading type="spinner" color="#fff" size="36px" />
                    <text class="loading-text">加入购物车中...</text>
                </view>
            </wd-overlay>

            <!-- 底部操作栏 -->
            <view class="bottom-bar">
                <view class="section-inner">
                    <view class="action-buttons">
                        <button class="action-btn add-cart" @click="handleAddToCart">加入购物车</button>
                        <button class="action-btn buy-now" @click="handleBuyNow">立即购买</button>
                    </view>
                </view>
            </view>
        </template>

        <!-- 参数弹窗 -->
        <wd-popup v-model="showParams" position="bottom" :round="true" safe-area-inset-bottom>
            <view class="params-popup">
                <view class="popup-header">
                    <text class="popup-title">规格参数</text>
                    <view class="close-icon" @click="showParams = false">
                        <wd-icon name="close" size="28rpx" color="#999" />
                    </view>
                </view>
                <scroll-view scroll-y class="popup-content">
                    <view class="param-tags">
                        <view class="param-tag" v-if="productDetail.modelNumber">
                            <text class="tag-label">型号</text>
                            <text class="tag-value">{{ productDetail.modelNumber }}</text>
                        </view>
                        <view class="param-tag" v-for="(param, index) in productDetail.parameters" :key="index">
                            <text class="tag-label">{{ param.split('：')[0] }}</text>
                            <text class="tag-value">{{ param.split('：')[1] }}</text>
                        </view>
                    </view>
                </scroll-view>
            </view>
        </wd-popup>

        <!-- 优惠券弹出框 -->
        <wd-popup v-model="showCouponsPopup" position="bottom" :round="true" safe-area-inset-bottom>
            <view class="coupons-popup">
                <view class="popup-header">
                    <text class="popup-title">优惠券</text>
                    <view class="close-icon" @click="showCouponsPopup = false">
                        <wd-icon name="close" size="28rpx" color="#999" />
                    </view>
                </view>
                <scroll-view scroll-y class="popup-content">
                    <view class="coupon-items">
                        <view class="coupon-item" v-for="coupon in productDetail.coupons" :key="coupon.id">
                            <view class="coupon-info" :class="{ 'discount-type': coupon.discountType === 2 }">
                                <view class="coupon-left">
                                    <view class="discount-value" v-if="coupon.discountType === 1">
                                        <text class="symbol">¥</text>
                                        <text class="value">{{ coupon.discountAmount }}</text>
                                    </view>
                                    <view class="discount-value" v-else>
                                        <text class="value">{{ Number(coupon.discountAmount) }}</text>
                                        <text class="symbol">折</text>
                                    </view>
                                    <view class="use-condition">{{ coupon.useCondition }}元可用</view>
                                </view>
                                <view class="coupon-right">
                                    <view class="coupon-name">{{ coupon.name }}</view>
                                    <view class="final-price" v-if="getCouponFinalPrice(coupon)">
                                        预计可省 ¥{{ getCouponFinalPrice(coupon) }}
                                    </view>
                                    <view class="valid-time">
                                        {{ coupon.startDate }} 至 {{ coupon.endDate }}
                                    </view>
                                </view>
                                <button class="get-btn" :class="{ 'disabled': coupon.canReceiveQuantity <= 0 }"
                                    @click.stop="handleGetCoupon(coupon)">
                                    {{ coupon.canReceiveQuantity <= 0 ? '已领完' : '立即领取' }} </button>
                            </view>
                        </view>
                    </view>
                </scroll-view>
            </view>
        </wd-popup>
    </view>


</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getProductDetail } from '@/api/product'
import { getCouponDetailList, receiveCoupon } from '@/api/coupon'
import { addItemToCart } from '@/api/cart'
import { formatPrice } from '@/utils/format'

// 初始化数据结构
const productDetail = ref({
    id: '',
    name: '',
    modelNumber: '',
    price: 0,
    originalPrice: 0,
    salesCount: 0,
    brandName: '',
    categoryName: '',
    mainImageUrl: '',
    detailImagesUrl: [],
    videoLink: '',
    parameters: [],
    features: '',
    coupons: []
})

// 初始化评论数据结构
const comments = ref([{
    id: '',
    user: {
        nickname: '',
        avatar: ''
    },
    rating: 0,
    content: '',
    createTime: '',
    images: [] // 添加 images 数组的初始化
}])

const commentStats = ref({
    total: 0,
    averageRating: 0,
    goodRatingPercentage: 0
})

const loading = ref(true)
const loadingOverlay = ref(false)

// 添加参数弹窗控制
const showParams = ref(false)

// 计算是否有参数
const hasParams = computed(() => {
    return productDetail.value.modelNumber || (productDetail.value.parameters && productDetail.value.parameters.length > 0)
})

// 添加预览参数计算属性
const previewParameters = computed(() => {
    return productDetail.value.parameters?.slice(0, 3) || []
})

// 添加计算属性，限制只显示2张详情图（加上主图共3张）
const displayImages = computed(() => {
    return productDetail.value.detailImagesUrl?.slice(0, 2) || []
})

// 修改状态变量
const currentIndex = ref(0)
const touchStartX = ref(0)
const touchStartY = ref(0)

// 添加新的响应式变量
const showCouponsPopup = ref(false)

// 监听弹窗显示状态
watch(showCouponsPopup, async (newVal) => {
    if (newVal) {
        // 弹窗打开时加载优惠券详情
        await loadCouponDetails()
    }
})

// 修改计算优惠后价格的逻辑
const getCouponFinalPrice = (coupon) => {
    const originalPrice = productDetail.value.price
    if (originalPrice < coupon.useCondition) {
        return 0
    }

    if (coupon.discountType === 1) { // 满减券
        return coupon.discountAmount
    } else { // 折扣券
        // 后端返回9.5表示打9.5折
        const savedAmount = originalPrice * (1 - coupon.discountAmount / 10)
        return savedAmount.toFixed(2)
    }
}

// 修改优惠券领取逻辑
const handleGetCoupon = async (coupon) => {
    // 检查是否可以领取
    if (coupon.canReceiveQuantity <= 0) {
        uni.showToast({
            title: '已达到领取上限',
            icon: 'none'
        })
        return
    }

    try {
        const data = await receiveCoupon(coupon.id)
        if (data) {
            uni.showToast({
                title: '领取成功',
                icon: 'success'
            })
            // 更新优惠券状态
            coupon.canReceiveQuantity--
        } else {
            uni.showToast({
                title: message || '领取失败',
                icon: 'none'
            })
        }
    } catch (error) {
        uni.showToast({
            title: '领取失败',
            icon: 'none'
        })
    }
}

// 修改获取优惠券详情方法
const loadCouponDetails = async () => {
    if (!productDetail.value?.coupons?.length) return

    try {
        // 显示加载状态
        uni.showLoading({
            title: '加载中...',
            mask: true
        })

        const couponIds = productDetail.value.coupons.map(c => c.id)
        const data = await getCouponDetailList(couponIds)

        if (data) {
            // 更新优惠券信息
            productDetail.value.coupons = productDetail.value.coupons.map(coupon => {
                const couponDetail = data.find(d => d.id === coupon.id)
                if (couponDetail) {
                    return {
                        ...coupon,
                        canReceiveQuantity: couponDetail.canReceiveQuantity,
                        name: couponDetail.name,
                        discountType: couponDetail.discountType,
                        discountAmount: couponDetail.discountAmount,
                        useCondition: couponDetail.minPurchaseAmount,
                        startDate: couponDetail.startDate,
                        endDate: couponDetail.endDate
                    }
                }
                return coupon
            })
        } else {
            uni.showToast({
                title: message || '获取优惠券信息失败',
                icon: 'none'
            })
        }
    } catch (error) {
        console.error('获取优惠券详情失败:', error)
        uni.showToast({
            title: '获取优惠券信息失败',
            icon: 'none'
        })
    } finally {
        uni.hideLoading()
    }
}

// 修改商品详情加载方法
const loadProductDetail = async (productId) => {
    loading.value = true
    try {
        const data = await getProductDetail(productId)

        if (data) {
            // 更新商品详情数据
            productDetail.value = {
                id: data.id || '',
                name: data.name || '',
                modelNumber: data.modelNumber || '',
                price: data.price || 0,
                originalPrice: data.originalPrice || 0,
                salesCount: data.salesCount || 0,
                brandName: data.brandName || '',
                categoryName: data.categoryName || '',
                mainImageUrl: data.mainImageUrl || '',
                detailImagesUrl: data.detailImagesUrl || [],
                videoLink: data.videoLink || '',
                parameters: data.parameters || [],
                features: data.features || '',
                coupons: data.coupons || []
            }

            // 处理评论数据
            if (data.comments && Array.isArray(data.comments)) {
                comments.value = data.comments.map(comment => ({
                    id: comment.id || '',
                    user: {
                        nickname: comment.anonymous ? '匿名用户' : (comment.userNickname || ''),
                        avatar: comment.avatarUrl || ''
                    },
                    rating: Number(comment.rating || 0),
                    content: comment.reviewText || '',
                    createTime: comment.createTime || '',
                    images: [] // 确保每个评论都有 images 数组
                }))
            } else {
                comments.value = [] // 如果没有评论数据，设置为空数组
            }

            // 更新评论统计数据
            const commentCount = data.commentCount || 0
            commentStats.value = {
                total: commentCount,
                averageRating: Number(data.rating || 0), // 确保是数字类型
                goodRatingPercentage: data.goodRatingPercentage
            }

            // 初始化时不加载优惠券详情,等点击弹窗时再加载
            if (data.coupons) {
                productDetail.value.coupons = data.coupons
            }
        }
    } catch (error) {
        uni.showToast({
            title: '获取商品详情失败',
            icon: 'none'
        })
        comments.value = [] // 错误时也设置为空数组
    } finally {
        loading.value = false
    }
}

onLoad((options) => {
    const productId = options.id

    if (productId) {
        loadProductDetail(productId)
    } else {
        loading.value = false
        uni.showToast({
            title: '商品ID不存在',
            icon: 'none'
        })
    }
})

const cart = ref(false)

// 修改立即购买方法
const handleBuyNow = () => {
    // 构建结算项数据
    const checkoutData = {
        checkoutItems: [{
            id: productDetail.value.id,
            name: productDetail.value.name,
            price: productDetail.value.price,
            quantity: 1, // 立即购买默认数量为1
            image: productDetail.value.mainImageUrl
        }],
        totalPrice: productDetail.value.price
    }

    // 将数据编码并传递到结算页面
    try {
        const encodedData = encodeURIComponent(JSON.stringify(checkoutData))
        uni.navigateTo({
            url: `/pages/order/checkout?cartItems=${encodedData}&cart=${cart.value}`
        })
    } catch (error) {
        console.error('跳转结算页面失败:', error)
        uni.showToast({
            title: '系统繁忙，请稍后再试',
            icon: 'none'
        })
    }
}

// 加入购物车
const handleAddToCart = async () => {

    loadingOverlay.value = true

    try {
        await addItemToCart({
            productId: productDetail.value.id,
            quantity: 1,
            price: productDetail.value.price
        })
        uni.showToast({
            title: '已加入购物车',
            icon: 'success'
        })
    } catch (error) {
        uni.showToast({
            title: '加入购物车失败',
            icon: 'none'
        })
    } finally {
        loadingOverlay.value = false
    }
}

// 查看全部评价
const viewAllComments = () => {
    if (productDetail.value.id) {
        uni.navigateTo({
            url: `/pages/product/comments?productId=${productDetail.value.id}`
        })
    }
}

// 处理轮播图切换
const handleSwiperChange = (e) => {
    currentIndex.value = e.detail.current
}

// 处理触摸开始
const handleTouchStart = (e) => {
    if (currentIndex.value === 2) {
        touchStartX.value = e.touches[0].clientX
        touchStartY.value = e.touches[0].clientY
    }
}

// 处理触摸结束
const handleTouchEnd = (e) => {
    if (currentIndex.value === 2) {
        const touchEndX = e.changedTouches[0].clientX
        const touchEndY = e.changedTouches[0].clientY

        const deltaX = touchEndX - touchStartX.value
        const deltaY = touchEndY - touchStartY.value

        if (deltaX < -100 && Math.abs(deltaX) > Math.abs(deltaY)) {
            uni.pageScrollTo({
                selector: '.detail-images',
                duration: 300
            })

            uni.showToast({
                title: '查看更多图片',
                icon: 'none',
                duration: 1500
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    min-height: 100vh;
    background-color: #f7f8fa;
    padding-bottom: 120rpx;
}

// 通用区块样式
.section {
    background: #fff;
    margin-bottom: 16rpx;
    padding: 24rpx 0;

    &:first-child {
        margin-top: 0;
    }
}

// 统一的内容包裹器
.section-inner {
    padding: 0 24rpx;
    position: relative;
}

// 轮播图样式
.product-swiper {
    width: 100%;
    height: 700rpx;
    border-radius: 16rpx;
    overflow: hidden;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

    .swiper-image {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    &.is-last {
        .swiper-image {
            position: relative;

            &::after {
                content: '';
                position: absolute;
                right: 0;
                top: 0;
                bottom: 0;
                width: 60rpx;
                background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.1));
            }
        }
    }
}

// 通用头部样式
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;

    .title {
        font-size: 28rpx;
        font-weight: bold;
        color: #333;
    }

    .count {
        font-size: 24rpx;
        color: #999;
        margin-left: 8rpx;
    }

    .right {
        display: flex;
        align-items: center;
        font-size: 24rpx;
        color: #999;
    }
}

.price-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6rpx;
    padding: 4rpx 0;
}

.price-section {
    display: flex;
    align-items: baseline;
}

.price-symbol {
    font-size: 28rpx;
    color: #ff4444;
    font-weight: bold;
}

.price-value {
    font-size: 48rpx;
    color: #ff4444;
    font-weight: bold;
    margin: 0 8rpx 0 4rpx;
    line-height: 1.2;
}

.original-price {
    font-size: 24rpx;
    color: #999;
    text-decoration: line-through;
    margin-left: 8rpx;
}

.sales-info {
    font-size: 24rpx;
    color: #999;
    background: #f5f6f7;
    padding: 4rpx 12rpx;
    border-radius: 20rpx;
}

.title-row {
    margin: 0 0 24rpx;
}

.product-name {
    font-size: 32rpx;
    color: #333;
    font-weight: bold;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
}

.tags-row {
    display: flex;
    flex-wrap: wrap;
    gap: 12rpx;
    margin-bottom: 8rpx;
}

.tag {
    font-size: 22rpx;
    padding: 6rpx 20rpx;
    border-radius: 6rpx;
    line-height: 1.4;
    font-weight: 500;
}

.brand-tag {
    color: #ff6b6b;
    background-color: rgba(255, 107, 107, 0.06);
    border: 1rpx solid rgba(255, 107, 107, 0.2);
}

.category-tag {
    color: #5c7cfa;
    background-color: rgba(92, 124, 250, 0.06);
    border: 1rpx solid rgba(92, 124, 250, 0.2);
}

// 评价预览样式优化
.rating-brief {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 24rpx;
    background: #f8f9fa;
    border-radius: 12rpx;
    margin-bottom: 20rpx;
    box-shadow: inset 0 0 0 1rpx rgba(0, 0, 0, 0.03);

    .rating-left {
        display: flex;
        align-items: center;

        .rating-score {
            font-size: 40rpx;
            font-weight: bold;
            color: #ff4444;
            margin-right: 12rpx;
            line-height: 1;
        }
    }

    .rating-right {
        .rating-desc {
            font-size: 24rpx;
            color: #666;
        }
    }
}

.comment-preview {
    .comment-item {
        padding: 24rpx 0;
        border-bottom: 1rpx solid rgba(0, 0, 0, 0.06);

        &:last-child {
            border-bottom: none;
            padding-bottom: 0;
        }

        &:first-child {
            padding-top: 0;
        }
    }

    .user-info {
        display: flex;
        align-items: center;
        margin-bottom: 12rpx;

        .avatar {
            width: 48rpx;
            height: 48rpx;
            border-radius: 50%;
            margin-right: 12rpx;
        }

        .nickname {
            font-size: 26rpx;
            color: #666;
            margin-right: 12rpx;
        }
    }

    .comment-content {
        font-size: 26rpx;
        color: #333;
        line-height: 1.5;
        margin-bottom: 12rpx;
    }

    .comment-images {
        display: flex;
        gap: 8rpx;

        .comment-image {
            width: 180rpx;
            height: 180rpx;
            border-radius: 8rpx;
        }
    }
}

// 商品特点样式优化
.features-list {
    display: flex;
    flex-direction: column;
    gap: 16rpx;
    padding: 4rpx 0;
}

.feature-item {
    display: flex;
    align-items: flex-start;
    gap: 12rpx;
    padding: 4rpx 0;

    .feature-text {
        font-size: 26rpx;
        color: #333;
        line-height: 1.6;
        flex: 1;
        padding-top: 2rpx;
    }
}

// 商品详情图片样式
.detail-images {
    margin-top: 16rpx;
}

.detail-image {
    width: 100%;
    display: block;
    margin-bottom: 20rpx;
    border-radius: 8rpx;

    &:last-child {
        margin-bottom: 0;
    }
}

// 底部操作栏样式
.bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100rpx;
    background: #fff;
    box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
    z-index: 100;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.action-buttons {
    display: flex;
    height: 100%;
    gap: 20rpx;
    padding: 0 8rpx;
}

.action-btn {
    flex: 1;
    height: 80rpx;
    margin: 10rpx 0;
    border: none;
    border-radius: 40rpx;
    font-size: 28rpx;
    font-weight: bold;
    transition: all 0.2s ease;

    &.add-cart {
        background: #fff0f0;
        color: #ff4444;
        border: 1rpx solid rgba(255, 68, 68, 0.2);
        position: relative;
        overflow: hidden;

        &:active {
            transform: scale(0.98);
            background: #ffe6e6;
        }
    }

    &.buy-now {
        background: #ff4444;
        color: #fff;
        position: relative;
        overflow: hidden;

        &:active {
            transform: scale(0.98);
            background: #ff3333;
        }
    }
}

// 修改评分星星样式
.rating-stars {
    display: flex;
    align-items: center;
    margin-top: 4rpx;
}

// 修改参数预览样式
.params-preview {
    margin-top: 12rpx;
    padding: 12rpx 0;
    border-top: 1rpx solid rgba(0, 0, 0, 0.06);

    .preview-params {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .params-list {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        gap: 8rpx;
        padding-right: 12rpx;
    }

    .param-item {
        background: #f7f8fa;
        padding: 4rpx 12rpx;
        border-radius: 4rpx;

        .param-value {
            font-size: 22rpx;
            color: #666;
            line-height: 1.2;
        }
    }

    .expand-icon {
        flex-shrink: 0;
    }
}

// 修改弹窗样式
.params-popup {
    max-height: 75vh;
    display: flex;
    flex-direction: column;
    background: #fff;

    .popup-header {
        flex-shrink: 0;
        padding: 28rpx 32rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;

        &::after {
            content: '';
            position: absolute;
            left: 32rpx;
            right: 32rpx;
            bottom: 0;
            height: 1rpx;
            background: #f5f5f5;
        }

        .popup-title {
            font-size: 28rpx;
            color: #333;
        }

        .close-icon {
            padding: 8rpx;
            margin: -8rpx;
        }
    }

    .popup-content {
        flex: 1;
        height: 0; // 关键：让scroll-view正确计算高度
    }

    .param-tags {
        padding: 16rpx 32rpx calc(16rpx + env(safe-area-inset-bottom));
    }

    .param-tag {
        display: flex;
        padding: 24rpx 0;
        position: relative;

        &::after {
            content: '';
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            height: 1rpx;
            background: #f5f5f5;
            transform: scaleY(0.5);
        }

        &:last-child::after {
            display: none;
        }

        .tag-label {
            font-size: 26rpx;
            color: #666;
            width: 140rpx;
            flex-shrink: 0;
        }

        .tag-value {
            flex: 1;
            font-size: 26rpx;
            color: #333;
            padding-left: 32rpx;
            line-height: 1.6;
        }
    }
}

// 添加无评价样式
.no-comment {
    padding: 32rpx 0;
    display: flex;
    justify-content: center;
    align-items: center;

    .no-comment-text {
        font-size: 26rpx;
        color: #999;
    }
}

// 添加优惠券展示区域
.coupons-row {
    display: flex;
    align-items: center;
    padding: 6rpx 0;
    margin-bottom: 10rpx;
}

.coupon-list {
    flex: 1;
    display: flex;
    gap: 12rpx;
    overflow-x: auto;
    padding: 4rpx 0;

    &::-webkit-scrollbar {
        display: none;
    }
}

.coupon-tag {
    padding: 4rpx 16rpx;
    border-radius: 4rpx;
    flex-shrink: 0;

    // 默认满减券样式
    background: #fff0f0;
    border: 1rpx solid rgba(255, 68, 68, 0.2);

    .coupon-name {
        color: #ff4444;
    }

    // 折扣券样式
    &.discount-type {
        background: #f0f5ff;
        border: 1rpx solid rgba(92, 124, 250, 0.2);

        .coupon-name {
            color: #5c7cfa;
        }
    }

    &.default-use {
        background: #ff4444;
        border-color: #ff4444;

        .coupon-name {
            color: #fff;
        }

        // 折扣券选中样式
        &.discount-type {
            background: #5c7cfa;
            border-color: #5c7cfa;
        }
    }
}

.coupon-name {
    font-size: 22rpx;
    line-height: 1.4;
}

.coupon-arrow {
    flex-shrink: 0;
    margin-left: 12rpx;
    padding: 8rpx;
}

// 修改优惠券弹出框样式
.coupons-popup {
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    background: #fff;

    .popup-header {
        padding: 32rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;

        &::after {
            content: '';
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            height: 2rpx;
            background: linear-gradient(to right, #f5f5f5, #e8e8e8, #f5f5f5);
        }

        .popup-title {
            font-size: 28rpx;
            font-weight: bold;
            color: #333;
        }

        .close-icon {
            padding: 8rpx;
            margin: -8rpx;
        }
    }

    .popup-content {
        flex: 1;
        max-height: calc(80vh - 96rpx);
    }

    .coupon-items {
        padding: 24rpx 24rpx calc(24rpx + env(safe-area-inset-bottom));
    }

    .coupon-item {
        margin-bottom: 24rpx;
        background: #fff;
        border-radius: 12rpx;
        position: relative;
        border: 1rpx solid rgba(255, 68, 68, 0.1);

        &:last-child {
            margin-bottom: 0;
        }

        // 添加优惠券锯齿边
        &::before,
        &::after {
            content: '';
            position: absolute;
            width: 16rpx;
            height: 100%;
            top: 0;
            background-image: radial-gradient(circle at 0 8rpx, transparent 0, transparent 6rpx, #fff 6rpx, #fff);
            background-size: 16rpx 16rpx;
            z-index: 1;
        }

        &::before {
            left: -8rpx;
        }

        &::after {
            right: -8rpx;
            transform: rotate(180deg);
        }
    }

    .coupon-info {
        display: flex;
        padding: 32rpx 24rpx;
        position: relative;
        align-items: center;
        gap: 24rpx;
        justify-content: space-between;

        // 默认满减券样式
        background: #fff0f0;

        // 折扣券样式
        &.discount-type {
            background: #f0f5ff;

            .coupon-left {
                border-right-color: rgba(92, 124, 250, 0.2);

                .discount-value {
                    color: #5c7cfa;
                }

                .use-condition {
                    color: #5c7cfa;
                }
            }

            .coupon-right {
                .final-price {
                    color: #5c7cfa;
                }
            }

            .get-btn {
                background: #5c7cfa;

                &.disabled {
                    background: #ddd;
                    color: #999;
                }
            }
        }
    }

    .coupon-left {
        flex-shrink: 0;
        width: 160rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 0 24rpx;
        border-right: 1rpx dashed rgba(255, 68, 68, 0.2);
        position: relative;
        z-index: 1;
        min-width: 160rpx;

        .discount-value {
            display: flex;
            align-items: baseline;
            color: #ff4444;

            .symbol {
                font-size: 24rpx;
                margin-right: 4rpx;
            }

            .value {
                font-size: 48rpx;
                font-weight: bold;
                line-height: 1;
            }
        }

        .use-condition {
            font-size: 22rpx;
            color: #ff4444;
            margin-top: 8rpx;
            white-space: nowrap;
        }
    }

    .coupon-right {
        flex: 1;
        padding: 0 24rpx;
        position: relative;
        z-index: 1;
        min-width: 0;
        max-width: calc(100% - 320rpx);

        .coupon-name {
            font-size: 28rpx;
            color: #333;
            font-weight: bold;
            margin-bottom: 12rpx;
            line-height: 1.4;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .final-price {
            font-size: 24rpx;
            color: #ff4444;
            font-weight: 500;
            margin-bottom: 12rpx;
        }

        .valid-time {
            font-size: 20rpx;
            color: #999;
            line-height: 1.4;
            display: flex;
            align-items: center;
            white-space: nowrap;
            overflow: visible;
            
            &::before {
                content: '有效期:';
                margin-right: 4rpx;
                flex-shrink: 0;
            }
        }
    }

    .get-btn {
        flex-shrink: 0;
        width: 120rpx;
        font-size: 24rpx;
        color: #fff;
        background: #ff4444;
        border: none;
        padding: 12rpx 32rpx;
        border-radius: 24rpx;
        line-height: 1.5;
        position: relative;
        overflow: hidden;
        min-width: 140rpx;
        text-align: center;
        margin-left: 0;

        // 添加回 disabled 样式
        &.disabled {
            background: #ddd;
            color: #999;
        }
    }
}

// 添加遮罩层中的加载样式
.loading-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16rpx;

    .loading-text {
        color: #fff;
        font-size: 28rpx;
    }
}
</style>